package com.ant.collect.service.impl;

import com.ant.collect.common.ResultCode;
import com.ant.collect.dto.SupplysideIndustryDto;
import com.ant.collect.handler.AntException;
import com.ant.collect.mapper.SupplysideAccessoriesMapper;
import com.ant.collect.mapper.SupplysideIndustryMapper;
import com.ant.collect.pojo.*;
import com.ant.collect.service.ISupplysideAccessoriesService;
import com.ant.collect.service.ISupplysideIndustryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hzd
 * @since 2025-10-31
 */
@Service
public class SupplysideIndustryServiceImpl extends ServiceImpl<SupplysideIndustryMapper, SupplysideIndustry> implements ISupplysideIndustryService {

    @Resource
    ISupplysideAccessoriesService accessoriesService;

    @Resource
    SupplysideAccessoriesMapper accessoriesMapper;

    @Override
    public SupplysideIndustry saveSupplysideIndustry(SupplysideIndustryDto dto) {
        if(dto.getCompanyId()==null){
            throw new AntException("请选择企业");
        }
        SupplysideIndustry year = new SupplysideIndustry();
        year.setIndustry(dto.getIndustry());
        year.setCompanyId(dto.getCompanyId());
        year.setIndustryName(dto.getIndustryName());
        year.setIndustryCode(dto.getIndustryCode());
        year.setProductName(dto.getProductName());
        year.setProductPrice(dto.getProductPrice());
        year.setVocUnit(dto.getVocUnit());
        year.setCreationTime(LocalDateTime.now());
        if(dto.getId()==null){
            baseMapper.insert(year);
        }else{
            baseMapper.updateById(year);
        }
        return year;
    }

    @Override
    public List<SupplysideIndustry> listById(Integer id) {
        return baseMapper.selectList(new QueryWrapper<SupplysideIndustry>().eq("company_id", id));
    }

    @Override
    public PageInfo<SupplysideIndustry> listPage(SupplysideIndustryDto dto) {
        try {
            PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
            QueryWrapper<SupplysideIndustry> wrapper = new QueryWrapper<>();
            if (dto.getCompanyId() != null) {
                wrapper.eq("company_id", dto.getCompanyId());
            }
            if (dto.getIndustry()!=null) {
                wrapper.eq("industry", dto.getIndustry());
            }
            if (dto.getIndustryName()!=null) {
                wrapper.eq("industry_name", dto.getIndustryName());
            }
            if (dto.getIndustryCode()!=null) {
                wrapper.eq("industry_code", dto.getIndustryCode());
            }
            if (dto.getProductName()!=null) {
                wrapper.eq("product_name", dto.getIndustryName());
            }

            wrapper.orderByDesc("id");
            List<SupplysideIndustry> list = this.baseMapper.selectList(wrapper);
            list.stream().forEach(item -> {
                List<SupplysideAccessories> tal =
                        accessoriesMapper.selectList(new QueryWrapper<SupplysideAccessories>()
                        .eq("product_id", item.getId()));
                item.setAccessoriesList(tal);
            });
            PageInfo<SupplysideIndustry> info = new PageInfo<>(list);
            return info;
        } catch (Exception e) {
            throw new AntException(ResultCode.BODY_NOT_MATCH);
        }
    }

    @Override
    public SupplysideIndustry findSupplySideIndustryDetails(Integer id) {
        SupplysideIndustry selectOne = baseMapper.selectOne(new QueryWrapper<SupplysideIndustry>().eq("id", id));
        List<SupplysideAccessories> tal = accessoriesMapper.selectList(new QueryWrapper<SupplysideAccessories>()
                .eq("product_id", id));
        selectOne.setAccessoriesList(tal);
        return selectOne;
    }
}
